package windows;

import Base.Dialog;
import global.Const;
import panel.common.LoadingBar;

import javax.swing.*;
import java.awt.*;

public class LoadingDialog extends Dialog implements Runnable {
    private LoadingBar loadingBar = new LoadingBar();  // 加载条
    private boolean isRunning = false;
    private static final int width = Const.WINDOW_WIDTH;
    private static final int height = Const.WINDOW_HEIGHT;

    public LoadingDialog() {
        init();
    }

    public void init() {
        initWindow();

        // 文字提示
        JLabel messageLabel = new JLabel("加载中，请稍等");
        messageLabel.setBounds(0, 50, 400, 100);
        messageLabel.setFont(new Font("微软雅黑", Font.PLAIN, 40));
        messageLabel.setHorizontalAlignment(SwingConstants.CENTER);
        messageLabel.setForeground(new Color(128, 128, 128));
        messageLabel.setBorder(null);
        // 添加组件
        window.add(messageLabel);
        window.add(loadingBar);// 加载条

        // 窗口默认关闭
        window.setVisible(false);
    }

    public JPanel getWindow(){
        return window;
    }

    public void open() {
        window.setVisible(true);
        setVisible(true);
        new Thread(this).start();
    }

    public void close() {
        window.setVisible(false);
        isRunning = false;
        setVisible(false);
    }

    @Override
    public void run() {
        System.out.print("加载中\n");
        isRunning = true;
        loadingBar.reset();
        while (isRunning) {
            loadingBar.moveBar();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


}
